<template>
  <div class="change-password">
    <h2>修改密码</h2>

    <form @submit.prevent="changePassword">
      <div class="form-group">
        <label for="oldPassword">原密码：</label>
        <input
            type="password"
            id="oldPassword"
            v-model="oldPassword"
            required
            :class="{ 'error-input': errors.oldPassword }"
            @blur="validateOldPassword"
        />
        <p v-if="errors.oldPassword" class="error-message">{{ errors.oldPassword }}</p>
      </div>

      <div class="form-group">
        <label for="newPassword">新密码：</label>
        <input
            type="password"
            id="newPassword"
            v-model="newPassword"
            required
            :class="{ 'error-input': errors.newPassword }"
            @input="validateNewPassword"
        />
        <p v-if="errors.newPassword" class="error-message">{{ errors.newPassword }}</p>
      </div>

      <div class="form-group">
        <label for="confirmPassword">确认新密码：</label>
        <input
            type="password"
            id="confirmPassword"
            v-model="confirmPassword"
            required
            :class="{ 'error-input': errors.confirmPassword }"
            @input="validateConfirmPassword"
        />
        <p v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</p>
      </div>

      <div class="form-actions">
        <button type="submit" :disabled="hasErrors">确认修改</button>
        <router-link to="/employee/dashboard" class="btn-back">返回</router-link>
      </div>
    </form>

    <!-- 新增：调试信息（开发环境使用，生产环境可删除） -->
<!--    <div v-if="debugMode" class="debug-info">-->
<!--      <p>错误状态：</p>-->
<!--      <pre>{{ errors }}</pre>-->
<!--      <p>按钮禁用条件：{{ hasErrors }}</p>-->
<!--    </div>-->

    <p v-if="message" class="message" :class="{ 'error': isError }">{{ message }}</p>
  </div>
</template>

<script>
export default {
  name: 'ChangePasswordEmployee',
  data() {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: '',
      message: '',
      isError: false,
      errors: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      debugMode: true // 启用调试模式，生产环境可设为false
    }
  },
  computed: {
    hasErrors() {
      // 简化判断逻辑，明确检查每个错误字段
      return !!(
          this.errors.oldPassword ||
          this.errors.newPassword ||
          this.errors.confirmPassword
      )
    }
  },
  methods: {
    validateOldPassword() {
      if (!this.oldPassword.trim()) {
        this.errors.oldPassword = '请输入原密码'
      } else {
        this.errors.oldPassword = '' // 明确清空错误
      }
    },

    validateNewPassword() {
      // 重置错误状态
      this.errors.newPassword = ''

      if (!this.newPassword) {
        this.errors.newPassword = '请输入新密码'
        return
      }

      if (this.newPassword.length < 6) {
        this.errors.newPassword = '新密码长度至少为6位'
        return
      }

      if (this.newPassword === this.oldPassword) {
        this.errors.newPassword = '新密码不能与原密码相同'
        return
      }

      // 如果新密码验证通过，同时验证确认密码
      if (this.confirmPassword) {
        this.validateConfirmPassword()
      }
    },

    validateConfirmPassword() {
      // 重置错误状态
      this.errors.confirmPassword = ''

      if (!this.confirmPassword) {
        this.errors.confirmPassword = '请确认新密码'
        return
      }

      if (this.confirmPassword !== this.newPassword) {
        this.errors.confirmPassword = '两次输入的新密码不一致'
        return
      }
    },

    validateForm() {
      // 提交前重新验证所有字段
      this.validateOldPassword()
      this.validateNewPassword()
      this.validateConfirmPassword()

      // 返回是否通过验证
      return !this.hasErrors
    },

    async changePassword() {
      // 表单验证
      if (!this.validateForm()) {
        console.log('表单验证失败，错误信息：', this.errors)
        return
      }

      const username = this.$store.state.user?.name
      if (!username) {
        this.message = '未找到当前用户'
        this.isError = true
        return
      }

      try {
        const response = await this.$axios.put('/auth/employee/change-password', null, {
          params: {
            name: username,
            oldPassword: this.oldPassword,
            newPassword: this.newPassword
          }
        })

        this.message = response.data || '密码修改成功'
        this.isError = false

        // 重置表单
        this.oldPassword = ''
        this.newPassword = ''
        this.confirmPassword = ''
        this.errors = { oldPassword: '', newPassword: '', confirmPassword: '' }
      } catch (error) {
        this.message = error.response?.data || '修改密码失败，请重试'
        this.isError = true
      }
    }
  }
}
</script>

<style scoped>
.change-password {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  font-weight: bold;
  margin-bottom: 5px;
}

input[type="password"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
  transition: border-color 0.3s;
}

input[type="password"]:focus {
  outline: none;
  border-color: #42b983;
}

.error-input {
  border-color: #ff4d4f !important;
}

.error-message {
  color: #ff4d4f;
  font-size: 14px;
  margin-top: 5px;
}

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 25px;
}

button {
  flex: 1;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  transition: background-color 0.3s;
}

button:hover:not(:disabled) {
  background-color: #369c6b;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.btn-back {
  flex: 1;
  text-align: center;
  text-decoration: none;
  color: #333;
  background-color: #eee;
  padding: 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.btn-back:hover {
  background-color: #ddd;
}

.message {
  margin-top: 20px;
  padding: 10px;
  border-radius: 4px;
  font-weight: bold;
  text-align: center;
}

.message:not(.error) {
  color: #155724;
  background-color: #d4edda;
  border: 1px solid #c3e6cb;
}

.message.error {
  color: #721c24;
  background-color: #f8d7da;
  border: 1px solid #f5c6cb;
}

/* 调试信息样式 */
.debug-info {
  margin-top: 15px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
  font-size: 12px;
  color: #666;
}

pre {
  margin: 5px 0;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>